package com.dominator.serviceImpl;

import com.dominFramework.core.id.SystemIdUtils;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.typewrap.Dtos;
import com.dominFramework.core.utils.SystemUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.mybatis.dao.T_userPO;
import com.dominator.mybatis.dao.T_wc2018_teamDao;
import com.dominator.mybatis.dao.T_wc2018_teamPO;
import com.dominator.repository.DaoTeam;
import com.dominator.repository.DaoUser;
import com.dominator.service.ApiWcService;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.api.ApiMessageUtil;
import com.dominator.utils.api.ApiUtils;
import com.dominator.utils.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ApiWcServiceImpl implements ApiWcService {

    @Autowired
    DaoUser userDao;

    @Autowired
    T_wc2018_teamDao tWc2018TeamDao;
    @Autowired
    DaoTeam teamDao;


    @Override
    public ApiMessage createTeam(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"phoneNum,teamName");
        T_userPO user = userDao.selectByPhoneNum(dto.getString("phoneNum"));
        if (SystemUtils.isEmpty(user)) {
            log.error("用户不存在");
            throw new ApiException(ReqEnums.REQ_USER_NOT_EXSIT);
        }
        T_wc2018_teamPO t_wc2018_teamPO =teamDao.selectByUserId(user.getId());
        if (SystemUtils.isNotEmpty(t_wc2018_teamPO)) {
            log.error("球队已存在");
            throw new ApiException(ReqEnums.TEAM_IS_EXSIT);
        }
        t_wc2018_teamPO =new T_wc2018_teamPO();
        t_wc2018_teamPO.setId(SystemIdUtils.uuid());
        t_wc2018_teamPO.setPhone_num(user.getPhone_num());
        t_wc2018_teamPO.setUser_id(user.getId());
        t_wc2018_teamPO.setTeam_name(dto.getString("teamName"));
        t_wc2018_teamPO.setCreate_time(new Date());
        t_wc2018_teamPO.setIs_valid("1");
        tWc2018TeamDao.insert(t_wc2018_teamPO);
        return ApiMessageUtil.success();
    }

    @Override
    public ApiMessage modifyTeam(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"phoneNum,teamName");
        T_userPO user = userDao.selectByPhoneNum(dto.getString("phoneNum"));
        if (SystemUtils.isEmpty(user)) {
            log.error("用户不存在");
            throw new ApiException(ReqEnums.REQ_USER_NOT_EXSIT);
        }

        T_wc2018_teamPO t_wc2018_teamPO =teamDao.selectByUserId(user.getId());
        if (SystemUtils.isEmpty(t_wc2018_teamPO)) {
            log.error("只有队长可以改对名");
            throw new ApiException(ReqEnums.TEAM_NOT_LEADER);
        }
        t_wc2018_teamPO.setTeam_name(dto.getString("teamName"));
        tWc2018TeamDao.updateByKey(t_wc2018_teamPO);
        return ApiMessageUtil.success();
    }

    @Override
    public ApiMessage getMyTeam(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"phoneNum");
        Dto back =teamDao.getMyTeam(dto);
        return  ApiMessageUtil.success(back);
    }

    @Override
    public ApiMessage getRankList(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"beginTime,endTime");
        return  ApiMessageUtil.success(teamDao.getRankList(dto));
    }

    @Override
    public ApiMessage getMyRankList(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"beginTime,endTime,phoneNum");
        return  ApiMessageUtil.success(teamDao.getMyRankList(dto));
    }

    @Override
    public ApiMessage getTeamRateList(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"phoneNum");
        T_userPO user = userDao.selectByPhoneNum(dto.getString("phoneNum"));
        if (SystemUtils.isEmpty(user)) {
            log.error("用户不存在");
            throw new ApiException(ReqEnums.REQ_USER_NOT_EXSIT);
        }
        //查询队长id
        String capitanId = queryCapitanId(user);
        return ApiMessageUtil.success(teamDao.getTeamRateList(capitanId));
    }

    @Override
    public ApiMessage getMyTeamMemberRate(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"beginTime,endTime,phoneNum");
        List<Dto>list = teamDao.getMyTeamMemberRate(dto);
        return ApiMessageUtil.success(list);
    }

    @Override
    public ApiMessage joinTeam(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"invitePhoneNum,phoneNum");
        T_userPO my = userDao.selectByPhoneNum(dto.getString("phoneNum"));
        if (SystemUtils.isEmpty(my)) {
            log.error("用户不存在");
            throw new ApiException(ReqEnums.REQ_USER_NOT_EXSIT);
        }
        //判断自己是否有推荐人
        if(SystemUtils.isNotEmpty(my.getPid())){
            log.error("您已经有战队了");
            throw new ApiException(ReqEnums.TEAM_IS_EXSIT);
        }
        //判断自己是不是队长
        T_wc2018_teamPO t_wc2018_teamPO =teamDao.selectByUserId(my.getId());
        if(SystemUtils.isNotEmpty(t_wc2018_teamPO)){
            log.error("您已经有战队了");
            throw new ApiException(ReqEnums.TEAM_IS_EXSIT);
        }
        //符合要求才加战队
        T_userPO leader = userDao.selectByPhoneNum(dto.getString("invitePhoneNum"));
        if (SystemUtils.isEmpty(leader)) {
            log.error("邀请人不存在");
            throw new ApiException(ReqEnums.REQ_USER_NOT_EXSIT);
        }
        my.setPid(leader.getId());
        userDao.updateByPrimaryKey(my);
        return ApiMessageUtil.success("加入成功!");
    }

    private String queryCapitanId(T_userPO user) {
        String capitanId="";
        if (SystemUtils.isEmpty(user.getPid())){
            T_wc2018_teamPO team = teamDao.selectByUserId(user.getId());
            if (SystemUtils.isEmpty(team)){
                log.error("未参加");
                throw new ApiException(ReqEnums.TEAM_NOT_TAKE_PART_IN);
            }else{
                capitanId=team.getUser_id();
            }
        }else {
            T_wc2018_teamPO team = teamDao.selectByUserId(user.getPid());
            if (SystemUtils.isEmpty(team)){
                log.error("未参加");
                throw new ApiException(ReqEnums.TEAM_NOT_TAKE_PART_IN);
            }else {
                capitanId= team.getUser_id();
            }
        }
        return capitanId;
    }


}
